# vi: ts=4

from TextViewer import ShowOutput
from ClearDialogs import *
import os, string
import etgui, CmdNo
import vcs
from vcs import SYSTEM, WINDOWS

def AddToSourceControl(files):
	if SYSTEM==WINDOWS:
		cmd = 'cleardlg -addtosrc ' + files
		vcs.system(cmd)
	else:
		comment = ShowAddToSourceDialog(files)
		if comment is None:
			return
		comment = vcs.escape_quotes(comment)
		cmd = 'cleartool mkelem -ci -c "%s" %s' % (comment, files)
		rc, out = getalloutput(cmd)
		if rc:
			ShowOutput(cmd, out)
	UpdateFileInfo(files)
def CheckOut(files):
	if SYSTEM==WINDOWS:
		cmd = 'cleardlg -checkout ' + files
		vcs.system(cmd)
	else:
		r = ShowCheckOutDialog(files)
		if r is None:
			return
		comment, reserved = r
		comment = vcs.escape_quotes(comment)
		opt = ''
		if not reserved:
			opt = opt + '-unreserved'
		cmd = 'cleartool co %s -c "%s" %s' % (opt, comment, files)
		rc, out = getalloutput(cmd)
		if rc:
			ShowOutput(cmd, out)
	UpdateFileInfo(files)
def CheckIn(files):
	if SYSTEM==WINDOWS:
		cmd = 'cleardlg -checkin ' + files
		vcs.system(cmd)
	else:
		comment = ShowCheckInDialog(files)
		if comment is None:
			return
		commentOpt = '-nc'
		if comment:
			commentOpt = '-c "%s"' % vcs.escape_quotes(comment)
		cmd = 'cleartool ci -ident %s %s' % (comment, files)
		rc, out = getalloutput(cmd)
		if rc:
			ShowOutput(cmd, out)
	UpdateFileInfo(files)
def CancelCheckOut(files):
	if SYSTEM==WINDOWS:
		cmd = 'cleardlg -uncheckout ' + files
		vcs.system(cmd)
	else:
		answer = etgui.ShowAlert(etgui.eAlertQuestion, "Keep copy of checkedout file?")
		if answer == CmdNo.cIdCancel:
			return
		opt = ''
		if answer == CmdNo.cIdYes:
			opt = '-keep'
		cmd = 'cleartool unco %s %s' % (opt, files)
		rc, out = getalloutput(cmd)
		if rc:
			ShowOutput(cmd, out)
	UpdateFileInfo(files)
def CheckOutDirectory(file):
	dir = os.path.dirname(file)
	if SYSTEM==WINDOWS:
		cmd = 'cleardlg -checkout ' + dir
		vcs.system(cmd)
	else:
		r = ShowCheckOutDialog(dir)
		if r is None:
			return
		comment, reserved = r
		comment = vcs.escape_quotes(comment)
		opt = ''
		if not reserved:
			opt = opt + '-unreserved'
		cmd = 'cleartool co %s -c "%s" %s' % (opt, comment, dir)
		rc, out = getalloutput(cmd)
		if rc:
			ShowOutput(cmd, out)
def CheckInDirectory(file):
	dir = os.path.dirname(file)
	if SYSTEM==WINDOWS:
		cmd = 'cleardlg -checkin ' + dir
		vcs.system(cmd)
	else:
		comment = ShowCheckInDialog(dir)
		if comment is None:
			return
		commentOpt = '-nc'
		if comment:
			commentOpt = '-c "%s"' % vcs.escape_quotes(comment)
		cmd = 'cleartool ci %s %s' % (commentOpt, dir)
		rc, out = getalloutput(cmd)
		if rc:
			ShowOutput(cmd, out)
def ClearDetails(projdir):
	if SYSTEM==WINDOWS:
		execasync('cleardetails %s' % projdir)
	else:
		pass
def ListVersions(file):
	cmd = 'cleartool lsvtree %s' % file
	rc, out = getalloutput(cmd)
	ShowOutput(cmd, out)	
def ListCheckouts(dir):
	if SYSTEM==WINDOWS:
		cmd = 'clearfindco %s' % dir
		execasync(cmd)
	else:
		cmd = 'cleartool lsco %s' %dir
		rc, out = getalloutput(cmd)
		ShowOutput(cmd,out)
def ListHistory(file):
	cmd = 'clearhistory %s' % file
	execasync(cmd)
def Describe(file):
	cmd  ='cleardescribe %s' % file
	execasync(cmd)
def DisplayVersionTree(file):
	if SYSTEM==WINDOWS:
		cmd = 'clearvtree %s' % file
	else:
		cmd = 'cleartool lsvtree -graphical %s' % file
	execasync(cmd)
def FindMerge():
	cmd = 'cleartool findmerge -graphical -all -flatest'
	execasync(cmd)
def MergeManager():
	if SYSTEM==WINDOWS:
		execasync('clearmrgman')
	else:
		execasync('xclearmrgman')
def CompareVersions(file, version):
	compareto = '-pred'
	if version:
		version = repair(version)
		compareto = file+'@@'+version
	cmd = 'cleartool diff -graphical %s %s' % (compareto, file)
	execasync(cmd)
def MergeVersions(file, version):
	if not vcs.is_writable(file):
		etgui.ShowAlert(etgui.eAlertNote, 'Cannot merge readonly files.')
		return
	version = repair(version)
	cmd = 'cleartool merge -to %s -graphical -qall %s@@%s' % (file, file, version)
	execasync(cmd)
def MakeDirectory():
	r = ShowMakeDirectoryDialog()
	if r is None:
		return
	comment, parent, dir = r
	comment = vcs.escape_quotes(comment)
	cmd = 'cleartool mkdir -c "%s" %s' % (comment, vcs.joinpath(parent,dir))
	rc, out = getalloutput(cmd)
	if rc:
		ShowOutput(cmd, out)
def MakeBranch(file, version):
	r = ShowMakeBranchDialog(file)
	if r is None:
		return
	comment, branch = r
	comment = vcs.escape_quotes(comment)
	if version:
		version = repair(version)
		file = file+'@@'+version
	cmd = 'cleartool mkbranch -c "%s" %s %s' % (comment, branch, file)
	rc, out = getalloutput(cmd)
	if rc:
		ShowOutput(cmd, out)
def MakeLabel(file, version):
	r = ShowMakeLabelDialog(file)
	if r is None:
		return
	comment, label = r
	comment = vcs.escape_quotes(comment)
	if version:
		version = repair(version)
		file = file+'@@'+version
	cmd = 'cleartool mklabel -c "%s" %s %s' % (comment, label, file)
	rc, out = getalloutput(cmd)
	if rc:
		ShowOutput(cmd, out)
def MakeAttribute(file, version):
	r = ShowMakeAttributeDialog(file)
	if r is None:
		return
	comment, name, value = r
	comment = vcs.escape_quotes(comment)
	if version:
		version = repair(version)
		file = file+'@@'+version
	cmd = 'cleartool mkattr -c "%s" %s %s %s' % (comment, name, value, file)
	rc, out = getalloutput(cmd)
	if rc:
		ShowOutput(cmd, out)
def ShowConfigRecord(target):
	if not target:
		return
	cmd = 'cleartool catcr -flat ' + target
	rc, out = getalloutput(cmd)
	ShowOutput(cmd, out)
def LabelOnExecutable(target):
	if not target:
		return
	r = ShowLabelOnExeDialog()
	if not r:
		return
	label, replace = r
	opt = ''
	if replace:
		opt = '-replace'
	cmd = 'cleartool mklabel %s -config %s %s' % (opt, target, label)
	rc, out = getalloutput(cmd)
	ShowOutput(cmd, out)
def ViewConfigSpec():
	cmd = 'cleartool catcs'
	rc, out = getalloutput(cmd)
	ShowOutput(cmd, out)
def EditConfigSpec():
	if SYSTEM==WINDOWS:
		vcs.system('cleartool edcs')
	else:
		cmd = 'cleartool catcs'
		rc, cs = getalloutput(cmd)
		if rc:
			ShowOutput(cmd,cs)
			return
		cs = ShowConfigSpecEditor(cs)
		if cs is None:
			return
		csFile = '.config_spec_edited'
		f = open(csFile,'wb')
		f.write(cs)
		f.close()
		cmd = 'cleartool setcs %s' % csFile
		rc,out = getalloutput(cmd)
		vcs.rm_f(csFile)
		if rc:
			ShowOutput(cmd,out)
def StartView():
	if SYSTEM==WINDOWS:
		cmd = 'cleardlg -startview'
		vcs.system(cmd)
	else:
		views = vcs.getoutput('cleartool lsview -short')
		if not views:
			return
		views = string.split(views)
		r = ShowSelectViewDialog(views)
		if not r:
			return
		view = r
		vcs.system('cleartool startview %s' % view)
def MountVob():
	if SYSTEM==WINDOWS:
		cmd = 'cleardlg -mount'
		vcs.system(cmd)
	else:
		pass
def UnmountVob():
	if SYSTEM==WINDOWS:
		cmd  ='cleardlg -unmount'
		vcs.system(cmd)
	else:
		pass
def Attache():
	execasync('attache -n')
def ClearHomeBase():
	execasync('clearhomebase')
def XClearCase():
	execasync('xclearcase')

# --- helper functions

def UpdateFileInfo(files):
	for file in string.split(files):
		etgui.Sniffaccess('update_file_info', '*/', file)

def execasync(cmd):
	etgui.gSystem.RunProgAsynch(cmd)

def getalloutput(cmd):
	vcs.clr_output()
	out = vcs.getoutput(cmd)
	rc = (vcs.err != '')
	return rc, out+vcs.err

_d = {
	'\0' : '/0',
	'\1' : '/1',
	'\2' : '/2',
	'\3' : '/3',
	'\4' : '/4',
	'\5' : '/5',
	'\6' : '/6',
	'\7' : '/7',
	'\a' : '/a',
	'\b' : '/b',
	'\f' : '/f',
	'\n' : '/n',
	'\r' : '/r',
	'\t' : '/t',
	'\v' : '/v',
	'\\' : '/'
}
def repair(v):
	vv = ''
	for c in v:
		if _d.has_key(c):
			vv = vv+_d[c]
		else:
			vv = vv+c
	return vv
